//
//  AyStatusToolBar.m
//  xWeibo
//
//  Created by alvis mac on 8/25/15.
//  Copyright (c) 2015 alvis. All rights reserved.
//

#import "AyStatusToolBar.h"
#import "UIImageExtend.h"
#import "AyDefine.h"
#import "AyStatusFrame.h"
#import "UIImageExtend.h"
#import "UIImageView+WebCache.h"
#import "AyStatusTopView.h"
#import "AyReweetStatusView.h"

@interface AyStatusToolBar()
@property (nonatomic, strong)  NSMutableArray * btns;
@property (nonatomic, strong)  NSMutableArray * lines;
@property (nonatomic, weak) UIButton *reweetBtn;
@property (nonatomic, weak) UIButton *commentBtn;
@property (nonatomic, weak) UIButton *attitudeBtn;
@end


@implementation AyStatusToolBar

- (NSMutableArray *)btns
{
    if (_btns == nil) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}

- (NSMutableArray *)lines
{
    if (_lines == nil) {
        _lines = [NSMutableArray array];
    }
    return _lines;
}


- (instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.userInteractionEnabled=YES;
        
        self.image = [UIImage resizedImageWithName:@"timeline_card_bottom_background"];
        self.highlightedImage = [UIImage resizedImageWithName:@"timeline_card_bottom_background_highlighted"];
        
        self.reweetBtn = [self createBtn:@"转发" image:@"timeline_icon_retweet" bgImage:@"timeline_card_leftbottom_highlighted"];
        self.commentBtn = [self createBtn:@"评论" image:@"timeline_icon_comment" bgImage:@"timeline_card_middlebottom_highlighted"];
        self.attitudeBtn = [self createBtn:@"赞" image:@"timeline_icon_unlike" bgImage:@"timeline_card_rightbottom_highlighted"];
        [self createLine];
        [self createLine];
    }
    return self;
}


- (UIButton *) createBtn:(NSString *) title image:(NSString *) image bgImage:(NSString *) bgImage{
    UIButton *btn=[[UIButton alloc] init];
     [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
     btn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    btn.adjustsImageWhenHighlighted = NO;
    [btn setBackgroundImage:[UIImage resizedImageWithName:bgImage] forState:UIControlStateHighlighted];
    [self addSubview:btn];
    [self.btns addObject:btn];
    return btn;
}

- (void) createLine{
    UIImageView *line=[[UIImageView alloc] init];
    line.image = [UIImage imageWithName:@"timeline_card_bottom_line"];
    [self addSubview:line];
    [self.lines addObject:line];
}

- (void)layoutSubviews{
    [super layoutSubviews];

    CGFloat dividerW = 2; // 分割线的宽度
    CGFloat btnH = self.frame.size.height;
    CGFloat btnw=(self.frame.size.width-self.lines.count*dividerW)/self.btns.count;
    for (int i=0; i<self.btns.count; i++) {
        UIButton *btn=self.btns[i];
        btn.frame=CGRectMake((btnw+dividerW)*i, 0, btnw, btnH);
    }
    
    for (int j=0; j<self.lines.count; j++) {
        UIImageView *imgView=self.lines[j];
        imgView.frame=CGRectMake(btnw*(j+1), 0, dividerW, btnH);
    }

}

- (void)setStatus:(AyStatus *)status{
    _status=status;
    [self setupBtn:self.reweetBtn originalTitle:@"转发" count:status.reposts_count];
    [self setupBtn:self.commentBtn originalTitle:@"评论" count:status.comments_count];
    [self setupBtn:self.attitudeBtn originalTitle:@"赞" count:status.attitudes_count];
}

- (void)setupBtn:(UIButton *)btn originalTitle:(NSString *)originalTitle count:(int)count
{
    if (count) {
        NSString *title = nil;
        if (count < 10000) { // 小于1W
            title = [NSString stringWithFormat:@"%d", count];
        } else {
            double countDouble = count / 10000.0;
            title = [NSString stringWithFormat:@"%.1f万", countDouble];
            title = [title stringByReplacingOccurrencesOfString:@".0" withString:@""];
        }
        [btn setTitle:title forState:UIControlStateNormal];
    } else {
        [btn setTitle:originalTitle forState:UIControlStateNormal];
    }
}


@end
